package com.xing.servlet;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day39bookshop
 * 2021/4/29 19:07
 */
@WebServlet(value = "/code")
public class CodeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建ValidateCode对象
        ValidateCode validateCode = new ValidateCode(120, 25, 4, 30);
        //获取验证码
        String code = validateCode.getCode();
        System.out.println("验证码："+code);
        req.getSession().setAttribute("code",code);
        //3.把图片响应给浏览器
        validateCode.write(resp.getOutputStream());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
